package com.huhu.manager;

import java.io.IOException;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.extension.tmx.TMXLoader;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.extension.tmx.util.exception.TMXLoadException;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.BuildableTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.graphics.Color;

import com.huhu.GameActivity;

public class ResourcesManager {

	private static final ResourcesManager INSTANCE = new ResourcesManager();

	public Engine engine;
	public BoundCamera camera;
	public GameActivity activity;
	public VertexBufferObjectManager vbom;

	// Splash
	public ITextureRegion splash_region;
	private BitmapTextureAtlas splashTextureAtlas;

	// Menu
	public ITextureRegion menu_background_region;
	public ITextureRegion menu_frame_region;
	public BuildableBitmapTextureAtlas menuTextureAtlas;
	public ITextureRegion menu_start_region;
	private BitmapTextureAtlas menuStartTextureAtlas;
	public ITextureRegion menu_options_region;
	private BitmapTextureAtlas menuOptionsTextureAtlas;
	public ITextureRegion menu_about_region;
	private BitmapTextureAtlas menuAboutTextureAtlas;
	public ITextureRegion menu_save_her_region;
	private BitmapTextureAtlas menuSaveHerTextureAtlas;
	public ITextureRegion menu_save_frame_region;
	private BitmapTextureAtlas menuSaveTextureAtlas;
	public ITextureRegion menu_back_region;
	private BitmapTextureAtlas menuBackTextureAtlas;

	// Menu Options
	public ITextureRegion menu_options_gear_region;
	private BitmapTextureAtlas menuOptionsGearTextureAtlas;
	public ITextureRegion menu_sound_region;
	private BitmapTextureAtlas menuSoundTextureAtlas;
	public ITextureRegion menu_music_region;
	private BitmapTextureAtlas menuMusicTextureAtlas;
	// Menu About
	public ITextureRegion menu_about_logo_region;
	private BitmapTextureAtlas menuAboutLogoTextureAtlas;

	public Font font;
	public Font gameFont;

	public ITextureRegion stage_select_frame_region;
	public ITextureRegion stage_select_tiny_block_region;
	public ITextureRegion stage_select_tiny_block_locked_region;
	public ITextureRegion stage_select_title1_region;
	public ITextureRegion stage_selection_bkggray_region;
	private BuildableBitmapTextureAtlas stageLevelTextureAtlas;

	// Game
	public ITiledTextureRegion player_region;
	private BuildableBitmapTextureAtlas playerTextureAtlas;

	public ITextureRegion platform1_region;
	public ITextureRegion platform2_region;
	public ITextureRegion platform3_region;
	public ITextureRegion coin_region;
	public ITextureRegion complete_window_region;
	public ITiledTextureRegion star_region;
	public ITextureRegion move_right_region;
	public ITextureRegion move_left_region;
	public ITextureRegion jump_region;
	public ITextureRegion back_region;
	public ITextureRegion gameover_window_region;
	public ITextureRegion replay_region;
	public ITextureRegion next_region;
	public ITextureRegion navigator_lose_region;
	public ITextureRegion navigator_win_region;
	public ITextureRegion lady_region;
	private BuildableBitmapTextureAtlas platformTextureAtlas;

	// Game Audio
	public Music jump_sound;
	public Music die_sound;

	// Level 1
	public ITextureRegion unknow_brickover_region;
	public ITextureRegion gold_region;
	public ITextureRegion stage1_1_hello;
	public ITextureRegion saw_top_red_region;
	public ITextureRegion saw_left_region;
	public ITextureRegion saw_right_region;
	public ITextureRegion saw_bottom_region;
	public ITextureRegion key_region;
	public ITextureRegion save_flat_region;
	public ITextureRegion rocket_left_region;
	public ITextureRegion rocket_right_region;
	public ITextureRegion rocket_top_region;
	private BuildableBitmapTextureAtlas rocketTextureAtlas;
	private BuildableBitmapTextureAtlas hudTextureAtlas;
	private BitmapTextureAtlas ladyTextureAtlas;
	private BitmapTextureAtlas saveFlagTextureAtlas;
	private BuildableBitmapTextureAtlas levelCompleteWindowTextureAtlas;
	public ITextureRegion dynamic_platform_region;
	private BitmapTextureAtlas dynamicPlatformTextureAtlas;
	private BitmapTextureAtlas stageHelloTextureAtlas;
	private BuildableBitmapTextureAtlas sawTextureAtlas;

	public ITextureRegion unknown_region;
	public BitmapTextureAtlas unknownTextureAtlas;

	public ITextureRegion cloud_normal_region;
	public ITextureRegion cloud_bad_region;
	private BitmapTextureAtlas cloudTextureAtlas;
	
	// Level 2
	public ITextureRegion trap_region;
	private BitmapTextureAtlas trapTextureAtlas;
	public ITextureRegion switch_machine_region;
	private BitmapTextureAtlas switchMachineTextureAtlas;
	public ITextureRegion flower_region;
	private BitmapTextureAtlas flowerTextureAtlas;
	public ITextureRegion spring_region;
	private BitmapTextureAtlas springTextureAtlas;
	
	// Level 3
	public ITextureRegion aircraft_region;
	private BitmapTextureAtlas aircraftTextureAtlas;
	public ITextureRegion clock_region;
	private BitmapTextureAtlas clockTextureAtlas;
	public ITextureRegion girl_region;
	private BitmapTextureAtlas girlTextureAtlas;
	public ITextureRegion round_bullet_region;
	private BitmapTextureAtlas roundBulletTextureAtlas;
	public ITextureRegion slime1_region;
	private BitmapTextureAtlas slime1TextureAtlas;
	public ITextureRegion fire_region;
	private BitmapTextureAtlas fireTextureAtlas;
	
	// Level 4
	public ITextureRegion saw_bottom_small_region;
	private BitmapTextureAtlas sawBottomSmallTextureAtlas;
	public ITextureRegion knock_trap_region;
	private BitmapTextureAtlas knockTrapTextureAtlas;
	
	// Level 5
	public ITextureRegion dynamic_4_region;
	public ITextureRegion dynamic_6_region;
	public ITextureRegion dynamic_14_region;
	private BuildableBitmapTextureAtlas dynamic_1_5_TextureAtlas;
	
	//level 6
	public ITextureRegion trap_1_6_region;
	private BitmapTextureAtlas trap_1_6TextureAtlas;
	
	private TMXTiledMap mTMXTileMap;

	// SplashScene
	public void loadSplashTexture() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		splashTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 2048, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		splash_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				splashTextureAtlas, activity, "splash/splash.jpg", 0, 0);
		splashTextureAtlas.load();
	}

	public void unLoadSplashTexture() {
		splashTextureAtlas.unload();
		splash_region = null;
	}

	// MenuScene
	public void loadMenuResources() {
		loadMenuGraphics();
		loadMenuAudio();
		loadMenuFonts();
	}

	public void loadMenuTextures() {
		menuTextureAtlas.load();
		menuStartTextureAtlas.load();
		menuOptionsTextureAtlas.load();
		menuAboutTextureAtlas.load();
		menuSaveTextureAtlas.load();
		menuSaveHerTextureAtlas.load();
		menuBackTextureAtlas.load();
		menuOptionsGearTextureAtlas.load();
		menuAboutLogoTextureAtlas.load();
		menuSoundTextureAtlas.load();
		menuMusicTextureAtlas.load();
		font.load();
	}

	public void unLoadMenuTextures() {
		menuTextureAtlas.unload();
		menuStartTextureAtlas.unload();
		menuOptionsTextureAtlas.unload();
		menuAboutTextureAtlas.unload();
		menuSaveTextureAtlas.unload();
		menuSaveHerTextureAtlas.unload();
		menuBackTextureAtlas.unload();
		menuOptionsGearTextureAtlas.unload();
		menuAboutLogoTextureAtlas.unload();
		menuSoundTextureAtlas.unload();
		menuMusicTextureAtlas.unload();
//		font.unload();
	}

	private void loadMenuGraphics() {
		// Static Texture
		menuTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 2048, 2048);
		menu_background_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuTextureAtlas, activity,
						"menu/menu_background.jpg");
		menu_start_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuTextureAtlas, activity,
						"menu/menu_start.png");
		menu_options_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuTextureAtlas, activity,
						"menu/menu_options.png");
		menu_frame_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuTextureAtlas, activity,
						"menu/menu_frame.png");

		try {
			menuTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			menuTextureAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		menuStartTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 512, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		menu_start_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuStartTextureAtlas, activity,
						"menu/menu_start.png", 0, 0);
		menuStartTextureAtlas.load();

		menuOptionsTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		menu_options_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuOptionsTextureAtlas, activity,
						"menu/menu_options.png", 0, 0);
		menuOptionsTextureAtlas.load();

		menuAboutTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		menu_about_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuAboutTextureAtlas, activity,
						"menu/menu_about.png", 0, 0);
		menuAboutTextureAtlas.load();

		menuSaveTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		menu_save_frame_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuSaveTextureAtlas, activity,
						"menu/menu_save_frame.png", 0, 0);
		menuSaveTextureAtlas.load();

		menuSaveHerTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 128, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		menu_save_her_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuSaveHerTextureAtlas, activity,
						"menu/menu_save_her1.png", 0, 0);
		menuSaveHerTextureAtlas.load();

		menuBackTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		menu_back_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuBackTextureAtlas, activity,
						"menu/menu_back.png", 0, 0);
		menuBackTextureAtlas.load();

		menuOptionsGearTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		menu_options_gear_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuOptionsGearTextureAtlas, activity,
						"menu/Option_gear.png", 0, 0);
		menuOptionsGearTextureAtlas.load();

		menuAboutLogoTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		menu_about_logo_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuAboutLogoTextureAtlas, activity,
						"menu/About_logo.png", 0, 0);
		menuAboutLogoTextureAtlas.load();

		// Dynamic Texture
		menuSoundTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		menu_sound_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuSoundTextureAtlas, activity,
						"menu/Switch_on.png", 0, 0);
		menuSoundTextureAtlas.load();

		menuMusicTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		menu_music_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuMusicTextureAtlas, activity,
						"menu/Switch_on.png", 0, 0);
		menuMusicTextureAtlas.load();
	}

	public void replaceSaveHerTexture(int i) {
		String path = "menu/menu_save_her" + i + ".png";
		menuSaveHerTextureAtlas.clearTextureAtlasSources();
		menu_save_her_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuSaveHerTextureAtlas, activity, path, 0, 0);
	}

	private void loadMenuAudio() {

	}

	private void loadMenuFonts() {
		FontFactory.setAssetBasePath("font/");
		final ITexture mainFontTexture = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		font = FontFactory.createFromAsset(activity.getFontManager(),
				mainFontTexture, activity.getAssets(), "UnrealTournament.ttf",
				50, false, Color.BLACK);
		font.load();
	}

	// LevelScene
	public void loadLevelResources() {
		loadLevelGraphics();
		loadLevelAudio();
		loadLevelFonts();
	}

	public void loadLevelTextures() {
		stageLevelTextureAtlas.load();
	}

	public void unLoadLevelTextures() {
		stageLevelTextureAtlas.unload();
	}

	private void loadLevelGraphics() {
		stageLevelTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 2048, 2048);
		stage_select_frame_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(stageLevelTextureAtlas, activity,
						"level/StageSelect_frame.png");
		stage_select_tiny_block_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(stageLevelTextureAtlas, activity,
						"level/StageSelect_tinyblock.png");
		stage_select_tiny_block_locked_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(stageLevelTextureAtlas, activity,
						"level/StageSelect_tinyblock_locked.png");
		stage_select_title1_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(stageLevelTextureAtlas, activity,
						"level/StageSelect_title1.png");
		stage_selection_bkggray_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(stageLevelTextureAtlas, activity,
						"level/StageSelector_bkg_gray.jpg");

		try {
			stageLevelTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			stageLevelTextureAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void loadLevelAudio() {

	}

	private void loadLevelFonts() {

	}

	// GameScene
	public void loadGameResources(int currentLevel) {
		switch (currentLevel) {
		case 1:
			loadGameGraphicsLevel_1();
			break;
		case 2:
			loadGameGraphicsLevel_2();
			break;
		case 3:
			loadGameGraphicsLevel_3();
			break;
		case 4:
			loadGameGraphicsLevel_4();
			break;
		case 5:
			loadGameGraphicsLevel_5();
			break;
		case 6:
			loadGameGraphicsLevel_6();
			break;
		}
		// loadGameGraphics();
		loadGameAudio();
		loadGameFonts();
	}

	private void loadGameGraphicsLevel_1() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		loadHudGraphics();
		loadLevelCompleteWindowGraphics();
		loadLadyGraphics();
		loadPlayerGraphics();

		loadDynamicPlatformGraphics();
		loadUnknownBrickoverGraphics();
		loadGoldGraphics();
		loadStageHelloGraphics();
		loadSawGraphics();
		loadKeyGraphics();
		loadSaveFlagGraphics();
		loadRocketGraphics();
	}
	

	public void unLoadGameTexturesLevel_1() {
		unLoadHudTexture();
		unLoadLevelCompleteWindowTexture();
		unLoadLadyTexture();
		unLoadPlayerTexture();
		unLoadDynamicPlatformTexture();
		unLoadUnknownBrickoverTexture();
		unLoadGoldTexture();
		unLoadStageHello();
		unLoadSawTexture();
		unLoadKeyTexture();
		unLoadSaveFlagTexture();
		unLoadRocketTexture();
		unLoadUnknowTexture();
		unLoadCloudTexture();
	}

	private void loadHudGraphics() {
		hudTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 512, 512);
		move_right_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(hudTextureAtlas, activity,
						"game/move_right.png");
		move_left_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(hudTextureAtlas, activity,
						"game/move_left.png");
		jump_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				hudTextureAtlas, activity, "game/jump.png");
		back_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				hudTextureAtlas, activity, "game/Thumb_back.png");

		try {
			hudTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			hudTextureAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void loadHudTexture() {
		hudTextureAtlas.load();
	}

	private void unLoadHudTexture() {
		hudTextureAtlas.unload();
	}

	private void loadLevelCompleteWindowGraphics() {
		levelCompleteWindowTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 2048, 2048);
		gameover_window_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(levelCompleteWindowTextureAtlas, activity,
						"game/gameover_window.png");
		replay_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				levelCompleteWindowTextureAtlas, activity,
				"game/Btn_replay.png");
		next_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				levelCompleteWindowTextureAtlas, activity, "game/Btn_next.png");
		navigator_lose_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(levelCompleteWindowTextureAtlas, activity,
						"game/Navigator_lose.png");
		navigator_win_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(levelCompleteWindowTextureAtlas, activity,
						"game/Navigator_win.png");

		try {
			levelCompleteWindowTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			levelCompleteWindowTextureAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void loadLevelCompleteWindowTexture() {
		levelCompleteWindowTextureAtlas.load();
	}

	private void unLoadLevelCompleteWindowTexture() {
		levelCompleteWindowTextureAtlas.unload();
	}

	public void loadLadyGraphics() {
		ladyTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(),
				64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		lady_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				ladyTextureAtlas, activity, "game/Lady.png", 0, 0);
		ladyTextureAtlas.load();
	}

	private void loadLadyTexture() {
		ladyTextureAtlas.load();
	}

	private void unLoadLadyTexture() {
		ladyTextureAtlas.unload();
	}
	public void replaceLadyTexture(){
//		ladyTextureAtlas.clearTextureAtlasSources();
//		lady_region
	}

	private void loadPlayerGraphics() {
		playerTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 256, 512);
		player_region = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(playerTextureAtlas, activity,
						"game/player.png", 2, 5);

		try {
			playerTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			playerTextureAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void loadPlayerTexture() {
		playerTextureAtlas.load();
	}

	private void unLoadPlayerTexture() {
		playerTextureAtlas.unload();
	}

	private void loadDynamicPlatformGraphics() {
		dynamicPlatformTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 128, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		dynamic_platform_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(dynamicPlatformTextureAtlas,
						activity.getAssets(), "game/DynamicPlatform.png", 0, 0);
		dynamicPlatformTextureAtlas.load();
	}

	private void loadDynamicPlatformTexture() {
		dynamicPlatformTextureAtlas.load();
	}

	private void unLoadDynamicPlatformTexture() {
		dynamicPlatformTextureAtlas.unload();
	}

	private BitmapTextureAtlas unknownBrickoverTextureAtlas;

	private void loadUnknownBrickoverGraphics() {
		unknownBrickoverTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 64, 64,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		unknow_brickover_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(unknownBrickoverTextureAtlas, activity,
						"game/level1/UnknownBrickOver.png", 0, 0);
		unknownBrickoverTextureAtlas.load();
	}

	private void loadUnknownBrickoverTexture() {
		unknownBrickoverTextureAtlas.load();
	}

	private void unLoadUnknownBrickoverTexture() {
		unknownBrickoverTextureAtlas.unload();
	}

	private BitmapTextureAtlas goldTextureAtlas;

	private void loadGoldGraphics() {
		goldTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(),
				64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		gold_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				goldTextureAtlas, activity, "game/level1/gold.png", 0, 0);
		goldTextureAtlas.load();
	}

	private void loadGoldTexture() {
		goldTextureAtlas.load();
	}

	private void unLoadGoldTexture() {
		goldTextureAtlas.unload();
	}

	private void loadStageHelloGraphics() {
		stageHelloTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 512, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		stage1_1_hello = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(stageHelloTextureAtlas, activity,
						"game/level1/Stage1_1_hello.jpg", 0, 0);
		stageHelloTextureAtlas.load();
	}

	private void loadStageHelloTexture() {
		stageHelloTextureAtlas.load();
	}

	private void unLoadStageHello() {
		stageHelloTextureAtlas.unload();
	}

	private void loadSawGraphics() {
		sawTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 256, 256);
		saw_top_red_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(sawTextureAtlas, activity,
						"game/level1/Saw_top_red.png");
		saw_left_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(sawTextureAtlas, activity,
						"game/level1/Saw_left_large.png");
		saw_right_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(sawTextureAtlas, activity,
						"game/level1/Saw_right_large.png");
		saw_bottom_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(sawTextureAtlas, activity, "game/level2/Saw_bottom.png");

		try {
			sawTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			sawTextureAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void loadSawTexture() {
		sawTextureAtlas.load();
	}

	private void unLoadSawTexture() {
		sawTextureAtlas.unload();
	}

	private BitmapTextureAtlas keyTextureAtlas;

	private void loadKeyGraphics() {
		keyTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(),
				64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		key_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				keyTextureAtlas, activity, "game/level1/Key.png", 0, 0);
		keyTextureAtlas.load();
	}

	private void loadKeyTexture() {
		keyTextureAtlas.load();
	}

	private void unLoadKeyTexture() {
		keyTextureAtlas.unload();
	}

	private void loadSaveFlagGraphics() {
		saveFlagTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 64, 64,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		save_flat_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(saveFlagTextureAtlas, activity,
						"game/level1/SaveFlag.png", 0, 0);
		saveFlagTextureAtlas.load();
	}

	private void loadSaveFlagTexture() {
		saveFlagTextureAtlas.load();
	}

	private void unLoadSaveFlagTexture() {
		saveFlagTextureAtlas.unload();
	}

	private void loadRocketGraphics() {
		rocketTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 512, 512);
		rocket_left_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(rocketTextureAtlas, activity,
						"game/level1/rocket_left.png");
		rocket_right_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(rocketTextureAtlas, activity,
						"game/level1/rocket_right.png");
		rocket_top_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(rocketTextureAtlas, activity,
						"game/level1/rocket_top.png");

		try {
			rocketTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			rocketTextureAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void loadRocketTexture() {
		rocketTextureAtlas.load();
	}

	private void unLoadRocketTexture() {
		rocketTextureAtlas.unload();
	}

	public void loadUnknownGraphics() {
		unknownTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 64, 64,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		unknown_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(unknownTextureAtlas, activity,
						"game/level1/Unknown.png", 0, 0);
		unknownTextureAtlas.load();
	}

	public void loadUnknownTexture() {
		unknownTextureAtlas.load();
	}

	private void unLoadUnknowTexture() {
		unknownTextureAtlas.unload();
	}

	public void replaceUnknownTexture() {
		unknownTextureAtlas.clearTextureAtlasSources();
		unknown_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(unknownTextureAtlas, activity,
						"game/level1/UnknownBrickOver.png", 0, 0);
	}

	public void loadCloudGraphics() {
		cloudTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		cloud_normal_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(cloudTextureAtlas, activity,
						"game/level1/Cloud_normal.png", 0, 0);
		cloudTextureAtlas.load();
	}

	public void loadCloudTexture() {
		cloudTextureAtlas.load();
	}

	private void unLoadCloudTexture() {
		cloudTextureAtlas.unload();
	}

	public void replaceCloudTexture() {
		cloudTextureAtlas.clearTextureAtlasSources();
		cloud_normal_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(cloudTextureAtlas, activity,
						"game/level1/Cloud_bad.png", 0, 0);
	}
	

	private void loadGameGraphicsLevel_2() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		loadHudGraphics();
		loadLevelCompleteWindowGraphics();
		loadLadyGraphics();
		loadPlayerGraphics();
		loadUnknownBrickoverGraphics();
		loadUnknownGraphics();
		loadCloudGraphics();
		loadTrapGraphics();
		loadStageHelloGraphics();
//		loadSwitchMachineGraphics();
		loadSawGraphics();
		loadFlowerGraphics();
		loadSaveFlagGraphics();
		loadSpringGraphics();
	}
	
	private void unLoadGameTexturesLevel_2(){
		unLoadHudTexture();
		unLoadLevelCompleteWindowTexture();
		unLoadLadyTexture();
		unLoadPlayerTexture();
		unLoadUnknownBrickoverTexture();
		unLoadUnknowTexture();
		unLoadCloudTexture();
		unLoadTrapTexture();
		unLoadStageHello();
		unLoadSawTexture();
		unLoadFlowerTexture();
		unLoadSaveFlagTexture();
		unLoadSpringTexture();
		
	}

	private void loadTrapGraphics() {
		trapTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		trap_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(trapTextureAtlas, activity, "game/level2/Stage1_2_hello.png", 0, 0);
		trapTextureAtlas.load();
	}

	private void loadTrapTexture() {
		trapTextureAtlas.load();
	}

	private void unLoadTrapTexture() {
		trapTextureAtlas.unload();
	}

	public void loadSwitchMachineGraphics() {
		switchMachineTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		switch_machine_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(switchMachineTextureAtlas, activity, "game/level2/Switch_machine_off.png", 0, 0);
		switchMachineTextureAtlas.load();
	}

	private void loadSwitchMachineTexture() {
		switchMachineTextureAtlas.load();
	}

	private void unLoadSwitchMachineTexture() {
		switchMachineTextureAtlas.unload();
	}
	public void replaceSwitchMachineTexture(){
		switchMachineTextureAtlas.clearTextureAtlasSources();
		switch_machine_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(switchMachineTextureAtlas, activity, "game/level2/Switch_machine_on.png", 0, 0);
	}
	private void loadFlowerGraphics(){
		flowerTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		flower_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(flowerTextureAtlas, activity, "game/level2/Flower.png", 0, 0);
		flowerTextureAtlas.load();
	}
	private void loadFlowerTexture(){
		flowerTextureAtlas.load();
	}
	private void unLoadFlowerTexture(){
		flowerTextureAtlas.unload();
	}
	
	private void loadSpringGraphics(){
		springTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		spring_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(springTextureAtlas, activity, "game/level2/Spring.png", 0, 0);
		springTextureAtlas.load();
	}
	private void loadSpringTexture(){
		springTextureAtlas.load();
	}
	private void unLoadSpringTexture(){
		splashTextureAtlas.unload();
	}
	
	private void loadGameGraphicsLevel_3(){
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		loadHudGraphics();
		loadLevelCompleteWindowGraphics();
		loadPlayerGraphics();
		loadAircraftGraphics();
		loadClockGraphics();
		loadGirlGraphics();
		loadRoundBulletGraphics();
		loadSlime1Graphics();
		loadFireGraphics();
	}
	
	private void unLoadGameTexturesLevel_3(){
		unLoadHudTexture();
		unLoadLevelCompleteWindowTexture();
		unLoadPlayerTexture();
		unLoadAircaftTexture();
		unLoadClockTexture();
		unLoadGirlTexture();
		unLoadRoundBulletTexture();
		unLoadSlime1Texture();
		unLoadFireTexture();
	}
	
	public void loadAircraftGraphics(){
		aircraftTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		aircraft_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(aircraftTextureAtlas, activity, "game/level3/Aircraft_normal.png", 0, 0);
		aircraftTextureAtlas.load();
	}
	private void unLoadAircaftTexture(){
		aircraftTextureAtlas.unload();
	}
	public void replaceAircraftDead(){
		aircraftTextureAtlas.clearTextureAtlasSources();
		aircraft_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(aircraftTextureAtlas, activity, "game/level3/Aircraft_dead.png", 0, 0);
	}
	
	public void loadClockGraphics(){
		clockTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		clock_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(clockTextureAtlas, activity, "game/level3/Clock_normal.png", 0, 0);
		clockTextureAtlas.load();
	}
	private void unLoadClockTexture(){
		clockTextureAtlas.unload();
	}
	public void replaceClockCry(){
		clockTextureAtlas.clearTextureAtlasSources();
		clock_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(clockTextureAtlas, activity, "game/level3/Clock_cry.png", 0, 0);
	}
	public void replaceClockDead(){
		clockTextureAtlas.clearTextureAtlasSources();
		clock_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(clockTextureAtlas, activity, "game/level3/Clock_dead.png", 0, 0);
	}
	private void loadGirlGraphics(){
		girlTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 128, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		girl_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(girlTextureAtlas, activity, "game/level3/menu_save_her1.png", 0, 0);
		girlTextureAtlas.load();
	}
	private void unLoadGirlTexture(){
		girlTextureAtlas.unload();
	}
	private void loadRoundBulletGraphics(){
		roundBulletTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 16, 16, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		round_bullet_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(roundBulletTextureAtlas, activity, "game/level3/RoundBullet.png", 0, 0);
		roundBulletTextureAtlas.load();
	}
	private void unLoadRoundBulletTexture(){
		roundBulletTextureAtlas.unload();
	}
	public void loadSlime1Graphics(){
		slime1TextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		slime1_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(slime1TextureAtlas, activity, "game/level3/Slime_1.png", 0, 0);
		slime1TextureAtlas.load();
	}
	private void unLoadSlime1Texture(){
		slime1TextureAtlas.unload();
	}
	public void replaceSlime1Texture(){
		slime1TextureAtlas.clearTextureAtlasSources();
		slime1_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(slime1TextureAtlas, activity, "game/level4/Slime_thorn.png", 0, 0);
	}
	
	private void loadFireGraphics(){
		fireTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		fire_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(fireTextureAtlas, activity, "game/level3/Thumb_dpad_fire.png", 0, 0);
		fireTextureAtlas.load();
	}
	private void unLoadFireTexture(){
		fireTextureAtlas.unload();
	}
	
	private void loadGameGraphicsLevel_4(){
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		loadHudGraphics();
		loadSawBottomSmallGraphics();
		loadKnockTrapGraphics();
		loadUnknownGraphics();
		loadLadyGraphics();
		loadSaveFlagGraphics();
		loadLevelCompleteWindowGraphics();
		loadRocketGraphics();
		loadPlayerGraphics();
//		loadSlime1Graphics();
	}
	
	private void unLoadGameTexturesLevel_4(){
		unLoadHudTexture();
		unLoadSawBottomSmallTexture();
		unLoadKnockTrapTexture();
		unLoadUnknowTexture();
		unLoadLadyTexture();
		unLoadSaveFlagTexture();
		unLoadLevelCompleteWindowTexture();
		unLoadRocketTexture();
		unLoadPlayerTexture();
	}
	
	private void loadSawBottomSmallGraphics(){
		sawBottomSmallTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 32, 16, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		saw_bottom_small_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(sawBottomSmallTextureAtlas, activity, "game/level4/Saw_bottom_small.png", 0, 0);
		sawBottomSmallTextureAtlas.load();
	}
	private void unLoadSawBottomSmallTexture(){
		sawBottomSmallTextureAtlas.unload();
	}
	private void loadKnockTrapGraphics(){
		knockTrapTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 320, 160, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		knock_trap_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(knockTrapTextureAtlas, activity, "game/level4/Knock_trap.png", 0, 0);
		knockTrapTextureAtlas.load();
	}
	private void unLoadKnockTrapTexture(){
		knockTrapTextureAtlas.unload();
	}
	
	private void loadGameGraphicsLevel_5(){
		loadHudGraphics();
		loadPlayerGraphics();
		loadLadyGraphics();
		loadSaveFlagGraphics();
		loadUnknownBrickoverGraphics();
		loadUnknownGraphics();
		loadGoldGraphics();
		loadRocketGraphics();
		loadLevelCompleteWindowGraphics();
		loadDynamicPlatformGraphics_1_5();
	}
	
	
	private void unLoadGameTexturesLevel_5(){
		unLoadHudTexture();
		unLoadSawBottomSmallTexture();
		unLoadKnockTrapTexture();
		unLoadUnknowTexture();
		unLoadLadyTexture();
		unLoadSaveFlagTexture();
		unLoadLevelCompleteWindowTexture();
		unLoadRocketTexture();
		unLoadPlayerTexture();
	}
	
	private void loadDynamicPlatformGraphics_1_5(){
		dynamic_1_5_TextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 512, 512);
		dynamic_4_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(dynamic_1_5_TextureAtlas, activity.getAssets(), "game/level5/DynamicPlatform_4.png");
		dynamic_6_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(dynamic_1_5_TextureAtlas, activity.getAssets(), "game/level5/DynamicPlatform_6.png");
		dynamic_14_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(dynamic_1_5_TextureAtlas, activity.getAssets(), "game/level5/DynamicPlatform_14.png");
		
		try {
			dynamic_1_5_TextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
			dynamic_1_5_TextureAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void loadGameGraphicsLevel_6(){
		loadHudGraphics();
		loadPlayerGraphics();
		loadLadyGraphics();
		loadUnknownBrickoverGraphics();
		loadUnknownGraphics();
		loadGoldGraphics();
		loadLevelCompleteWindowGraphics();
		loadTrap_1_6Graphics();
		
	}
	
	private void unLoadGameTexturesLevel_6(){
		unLoadHudTexture();
		unLoadPlayerTexture();
		unLoadLadyTexture();
		unLoadUnknownBrickoverTexture();
		unLoadUnknowTexture();
		unLoadGoldTexture();
		unLoadLevelCompleteWindowTexture();
		unLoadTrap_1_6Texture();
	}
	
	private void loadTrap_1_6Graphics(){
		trap_1_6TextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		trap_1_6_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(trap_1_6TextureAtlas, activity, "game/level6/trap_1_6.png", 0, 0);
		trap_1_6TextureAtlas.load();
	}
	
	private void unLoadTrap_1_6Texture(){
		trap_1_6TextureAtlas.unload();
	}

	private void loadGameAudio() {
		MusicFactory.setAssetBasePath("mfx/");
		try {
			jump_sound = MusicFactory.createMusicFromAsset(
					activity.getMusicManager(), activity, "Audio_jump.mp3");
			die_sound = MusicFactory.createMusicFromAsset(
					activity.getMusicManager(), activity, "Audio_boos.mp3");
			jump_sound.setLooping(false);
			die_sound.setLooping(false);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void loadGameFonts() {
		FontFactory.setAssetBasePath("font/");
		final ITexture gameFontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		gameFont = FontFactory.createFromAsset(activity.getFontManager(), gameFontTexture, activity.getAssets(), "Plok.ttf", 20, false, Color.BLACK);
		gameFont.load();
	}

	public void unLoadGameTextures(int currentLevel) {
		switch (currentLevel) {
		case 1:
			unLoadGameTexturesLevel_1();
			break;
		case 2:
			unLoadGameTexturesLevel_2();
			break;
		case 3:
			unLoadGameTexturesLevel_3();
			break;
		case 4:
			unLoadGameTexturesLevel_4();
			break;
		case 5:
			unLoadGameTexturesLevel_5();
			break;
		case 6:
			unLoadGameTexturesLevel_6();
			break;
		}
	}

	// Load map
	public TMXTiledMap loadMap(int level) {
		final TMXLoader mTMXLoader = new TMXLoader(activity.getAssets(),
				activity.getTextureManager(),
				TextureOptions.BILINEAR_PREMULTIPLYALPHA, vbom, null);
		try {
			switch (level) {
			case 1:
				mTMXTileMap = mTMXLoader.loadFromAsset("tmx/map_1_1.tmx");
				break;
			case 2:
				mTMXTileMap = mTMXLoader.loadFromAsset("tmx/map_1_2.tmx");
				break;
			case 3:
				mTMXTileMap = mTMXLoader.loadFromAsset("tmx/map_1_3.tmx");
				break;
			case 4:
				mTMXTileMap = mTMXLoader.loadFromAsset("tmx/map_1_4.tmx");
				break;
			case 5:
				mTMXTileMap = mTMXLoader.loadFromAsset("tmx/map_1_5.tmx");
				break;
			case 6:
				mTMXTileMap = mTMXLoader.loadFromAsset("tmx/map_1_6.tmx");
				break;
			}
		} catch (TMXLoadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return mTMXTileMap;
	}

	public static void prepareManager(Engine engine, GameActivity activity,
			BoundCamera camera, VertexBufferObjectManager vbom) {
		getInstance().engine = engine;
		getInstance().activity = activity;
		getInstance().camera = camera;
		getInstance().vbom = vbom;
	}

	public static ResourcesManager getInstance() {
		return INSTANCE;
	}
}
